package licode.unisop.client.feign.impl;

import licode.unisop.client.feign.SoUserVisitFeign;
import licode.unisop.client.info.SoUserAccountIn;
import licode.unisop.client.info.SopUserIdFind;
import licode.unisop.client.info.SopUserIdIn;
import licode.unisop.client.visit.UniClientTool;
import licode.unisop.client.visit.UserQueryVisit;
import licode.unisop.client.vo.SopAuthPlatform;
import licode.unisop.client.vo.SopUser;
import licode.unisop.client.vo.SopUserBrief;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户操作服务类
 *
 * @author licode
 */
@Component
public class UserQueryVisitFeignImpl implements UserQueryVisit {
    @Resource
    private HttpServletRequest request;

    @Resource
    private UniClientTool uniClientTool;

    @Resource
    private SoUserVisitFeign soUserVisitFeign;

    @Override
    public SopUser lookupUserById(SopUserIdIn userIdIn) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, userIdIn.getPlatformId());
        userIdIn.setRealmId(authPlatform.getRealmId());
        SopUserIdFind sopUserId = SopUserIdFind.builder()
                .realmId(userIdIn.getRealmId())
                .userId(userIdIn.getUserId()).build();

        return soUserVisitFeign.lookupUserById(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), sopUserId).getData();
    }

    @Override
    public SopUser lookupUserByAccount(SoUserAccountIn account) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, account.getPlatformId());
        account.setRealmId(authPlatform.getRealmId());
        return soUserVisitFeign.lookupUserByAccount(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), account).getData();
    }

    @Override
    public SopUserBrief lookupUserBriefById(SopUserIdIn userIdIn) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, userIdIn.getPlatformId());
        userIdIn.setRealmId(authPlatform.getRealmId());
        SopUserIdFind sopUserId = SopUserIdFind.builder()
                .realmId(userIdIn.getRealmId())
                .userId(userIdIn.getUserId()).build();

        return soUserVisitFeign.lookupUserBriefById(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), sopUserId).getData();
    }

    @Override
    public SopUserBrief lookupUserBriefByAccount(SoUserAccountIn account) {
        SopAuthPlatform authPlatform;
        authPlatform = uniClientTool.findAuthPlatform(request, account.getPlatformId());
        account.setRealmId(authPlatform.getRealmId());
        return soUserVisitFeign.lookupUserBriefByAccount(authPlatform.getPlatformId(),
                authPlatform.getModelToken(), account).getData();
    }
}
